"""
Case Type   : fullsql
Case Name   : track_stmt_session_slot参数gs_guc reload异常输入校验
Create At   : 2022/02/07
Owner       : zhao-pengcheng-g
Description :
    1.设置为最小值0，查看参数
    2.设置为INT_MAX，查看参数
    3.设置为-1，查看参数
    4.设置为2147483647000，查看参数
    5.设置为字符类型，查看参数
    6.设置为特殊字符，查看参数
    7.track_stmt_session_slot参数set异常输入校验
    8.恢复参数
Expect      :
    1.设置为0成功，查看参数为0
    2.设置为INT_MAX成功，查看参数为2147483647
    3.设置为-1失败，合理报错，查看参数为2147483647
    4.设置为2147483647000失败，合理报错，查看参数为2147483647
    5.设置为字符类型失败，合理报错，查看参数为2147483647
    6.设置为特殊字符失败，合理报错，查看参数为2147483647
    7 .设置失败，合理报错，
    8.恢复参数成功
History     :
"""

import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class Fullsql(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        text = '---Opengauss_Function_Fullsql_Case0030 start---'
        self.logger.info(text)
        self.userNode = Node('PrimaryDbUser')
        self.sh_primy = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()

    def test_fullsql(self):
        text = '---step1:设置为最小值0，查看参数,' \
               'expect:设置成功,查看参数结果为0---'
        self.logger.info(text)
        self.config_slot = 'track_stmt_session_slot'
        self.show_slot_cmd = f'show {self.config_slot};'
        check_slot = self.sh_primy.execut_db_sql(self.show_slot_cmd)
        self.logger.info(check_slot)
        self.slot_default = check_slot.splitlines()[-2].strip()
        guc_slot = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_slot}=0")
        self.assertTrue(guc_slot, '执行失败:' + text)
        show_slot_msg = self.sh_primy.execut_db_sql(self.show_slot_cmd)
        self.logger.info(show_slot_msg)
        self.common.equal_sql_mdg(show_slot_msg, self.config_slot,
                                  '0', '(1 row)', flag='1')

        text = "---step2:设置为INT_MAX，查看参数," \
               "expect:设置成功,查看参数结果为2147483647---"
        self.logger.info(text)
        guc_slot = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_slot}=2147483647")
        self.assertTrue(guc_slot, '执行失败:' + text)
        show_slot_msg = self.sh_primy.execut_db_sql(self.show_slot_cmd)
        self.logger.info(show_slot_msg)
        self.common.equal_sql_mdg(show_slot_msg, self.config_slot,
                                  '2147483647', '(1 row)', flag='1')

        text = '---step3:设置为-1，查看参数,expect:设置失败，合理报错，' \
               '查看参数结果为2147483647---'
        self.logger.info(text)
        guc_slot = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_slot}=-1",
                                               get_detail=True)
        self.assertIn(self.constant.OUTSIDE_VALID_RANGE_MSG,
                      guc_slot, '执行失败:' + text)
        show_slot_msg = self.sh_primy.execut_db_sql(self.show_slot_cmd)
        self.logger.info(show_slot_msg)
        self.common.equal_sql_mdg(show_slot_msg, self.config_slot,
                                  '2147483647', '(1 row)', flag='1')

        text = '---step4:设置为2147483647000，查看参数,' \
               'expect:设置失败，合理报错，查看参数结果为2147483647---'
        self.logger.info(text)
        guc_slot = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_slot}=2147483647000",
                                               get_detail=True)
        self.assertIn(self.constant.OUTSIDE_VALID_RANGE_MSG,
                      guc_slot, '执行失败:' + text)
        show_slot_msg = self.sh_primy.execut_db_sql(self.show_slot_cmd)
        self.logger.info(show_slot_msg)
        self.common.equal_sql_mdg(show_slot_msg, self.config_slot,
                                  '2147483647', '(1 row)', flag='1')

        text = '---step5:设置为字符值，查看参数,' \
               'expect:设置失败，合理报错，查看参数结果为2147483647---'
        self.logger.info(text)
        guc_slot = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_slot}='abcdef'",
                                               get_detail=True)
        self.assertIn(self.constant.requires_integer_value_msg,
                      guc_slot, '执行失败:' + text)
        show_slot_msg = self.sh_primy.execut_db_sql(self.show_slot_cmd)
        self.logger.info(show_slot_msg)
        self.common.equal_sql_mdg(show_slot_msg, self.config_slot,
                                  '2147483647', '(1 row)', flag='1')

        text = '---step6:设置为特殊字符，查看参数,' \
               'expect:设置失败，合理报错，查看参数结果为2147483647---'
        self.logger.info(text)
        guc_slot = self.sh_primy.execute_gsguc('reload',
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               f"{self.config_slot}='@@$%^^!!'",
                                               get_detail=True)
        self.assertIn(self.constant.requires_integer_value_msg,
                      guc_slot, '执行失败:' + text)
        show_slot_msg = self.sh_primy.execut_db_sql(self.show_slot_cmd)
        self.logger.info(show_slot_msg)
        self.common.equal_sql_mdg(show_slot_msg, self.config_slot,
                                  '2147483647', '(1 row)', flag='1')

        text = '---step7:track_stmt_session_slot参数set异常输入校验，' \
               '查看参数,expect:设置失败，合理报错，查看参数结果为2147483647---'
        self.logger.info(text)
        set_cmd = f"set {self.config_slot}=-1;"
        set_msg = self.sh_primy.execut_db_sql(set_cmd)
        self.logger.info(set_msg)
        self.assertIn(f"parameter \"{self.config_slot}\" cannot be "
                      f"changed now", set_msg, '执行失败:' + text)
        show_slot_msg = self.sh_primy.execut_db_sql(self.show_slot_cmd)
        self.logger.info(show_slot_msg)
        self.common.equal_sql_mdg(show_slot_msg, self.config_slot,
                                  '2147483647', '(1 row)', flag='1')

    def tearDown(self):
        text = '---step8:恢复参数，expect:成功---'
        self.logger.info(text)
        check_slot = self.sh_primy.execut_db_sql(self.show_slot_cmd)
        self.logger.info(check_slot)
        current = check_slot.splitlines()[-2].strip()
        if self.slot_default != current:
            result = self.sh_primy.execute_gsguc('reload',
                                                 self.constant.GSGUC_SUCCESS_MSG,
                                                 f'{self.config_slot}={self.slot_default}')
            self.assertTrue(result, '参数恢复失败')
        show_slot_msg = self.sh_primy.execut_db_sql(self.show_slot_cmd)
        self.logger.info(show_slot_msg)
        self.common.equal_sql_mdg(show_slot_msg, self.config_slot,
                                  self.slot_default, '(1 row)', flag='1')
        self.logger.info(
            '---Opengauss_Function_Fullsql_Case0030 finish---')
